import java.util.ArrayList;
import java.util.Random;

public class PlayingCard {
    //首先我们需要设置一个类来表示每张扑克牌
    static class Card {
        //一张牌包括点数和花色
        String suit;
        String rank;

        public Card(String suit, String rank) {
            this.suit = suit;
            this.rank = rank;
        }

        public String getSuit() {
            return suit;
        }

        public void setSuit(String suit) {
            this.suit = suit;
        }

        public String getRank() {
            return rank;
        }

        public void setRank(String rank) {
            this.rank = rank;
        }

        public String toString() {
            return suit + "" + rank;
        }
    }

    //接着我们要写一个方法来创建一副扑克牌
    public static ArrayList<Card> createDeck() {
        ArrayList<Card> deck = new ArrayList<>();
        String[] suits = {"♠", "♥", "♣", "♦"};
        //每次循环都将点数和花色存放到cards中
        for (String suit : suits) {
            for (int i = 2; i <= 10; i++) {
                Card card = new Card(suit, ("" + i));
                deck.add(card);
            }
            Card cardJ = new Card(suit, "J");
            Card cardQ = new Card(suit, "Q");
            Card cardK = new Card(suit, "K");
            Card cardA = new Card(suit, "A");
            deck.add(cardJ);
            deck.add(cardQ);
            deck.add(cardK);
            deck.add(cardA);
        }
        return deck;
    }

    //接着我们需要洗牌
    private static void shuffle(ArrayList<Card> deck) {
        Random random = new Random();
        //遍历整个ArrayList
        for (int i = 0; i < deck.size(); i++) {
            //生成随机数，将当前下标与随机下标进行交换
            int ret = random.nextInt(deck.size());
            //这里的卡片需要重新new一个对象以防止对原来顺序表的改变
            Card temp = new Card(deck.get(i).getSuit(),deck.get(i).getRank());
            deck.set(i, deck.get(ret));
            deck.set(ret,temp);
        }
    }


    public static void main(String[] args) {
        ArrayList<Card> deck = createDeck();
        System.out.println("交换前扑克牌顺序为：");
        System.out.println(deck);
        shuffle(deck);
        System.out.println("交换后扑克牌顺序为：");
        System.out.println(deck);
        //洗完牌之后开始发牌,发牌轮流发，需要给三个人每人发五张牌
        ArrayList<ArrayList<Card>> handCards = new ArrayList<ArrayList<Card>>();
        //先创建三个人手里的位置****************
        for (int i = 0; i < 3; i++) {
            handCards.add(new ArrayList<>());
        }
        //创建好空盒子之后再将牌分别分发给每个人
        for(int i = 0; i < 5; i++){
            for(int j = 0; j < 3; j++){
                handCards.get(j).add(deck.remove(0));
            }
        }
        //打印出每个人手中的牌
        for (int i = 0; i < 3; i++){
            System.out.println("第"+i+"个人的手牌为："+handCards.get(i));
        }
    }
}
